package com.neuedu.jdbc;

import com.neuedu.jdbc.util.DBUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author 金山
 * 项目：jdbc
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2024/11/811:52
 */
public class TX {


    public static void main(String[] args) {
        {

            Connection conn = null;
            Statement stmt = null;
            ResultSet rs = null;
            try {
                conn = DBUtils.getConnection();


                //设置自动提交为 false
                conn.setAutoCommit(false);


                //3 创建statement
                stmt = conn.createStatement();

                //4 执行sql语句  结果集 rs
                StringBuffer sql  = new StringBuffer();
                sql.append("UPDATE  account SET amount = amount - 500 WHERE id = 1  " );
                int count1 = stmt.executeUpdate(sql.toString());
                System.out.println("更新影响的行数 count1 = " + count1);


                int num = 0;
                long result = num/num;
                System.out.println(result);


                StringBuffer sql2  = new StringBuffer();
                sql2.append("UPDATE  account SET amount = amount + 500 WHERE id = 2  " );
                int count2 = stmt.executeUpdate(sql2.toString());
                System.out.println("更新影响的行数 count2 = " + count2);


                //提交数据 手动提交
                conn.commit();

            } catch ( SQLException e) {
                //回滚数据
                try {
                    conn.rollback();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
                throw new RuntimeException(e);
            }finally {
                //   6 关闭资源
                DBUtils.close(conn,stmt,null);
            }





        }

    }


}
